package com.example.admin.goodnews.activity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.adapter.MoreChannelQuanBuAdapter;
import com.example.admin.goodnews.adapter.MoreChannelYiXuanAdapter;
import com.example.admin.goodnews.choosecity.MyGridView;
import com.example.admin.goodnews.database.MyDBHelper;
import com.example.admin.goodnews.database.MyDBManager;
import com.example.admin.goodnews.entity.NewsClassEntity;
import com.example.admin.goodnews.event.DBChangeEvent;
import com.example.admin.goodnews.fragment.BaseFragment;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 频道管理页面
 * Created by admin on 2016/10/20.
 */

public class ChannelManageActivity extends AppCompatActivity {
    private MyGridView mygridview_yixuan;// 已选频道
    private MyGridView mygridview_quanbu;// 全部频道
    private NewsClassEntity newsClassEntity;
    private List<String> defaultlists = new ArrayList<>();// 默认已选频道分类
    private List<Fragment> defaultfragmentlists = new ArrayList<>();// 默认已选频道页卡
    private List<String> tablists = new ArrayList<>();// 显示已选频道用的列表
    private List<String> alltablists = new ArrayList<>();// 全部新闻频道分类
    private List<String> remaintablists = new ArrayList<>();// 全部新闻频道分类去除已选新闻分类后的新闻分类
    private List<BaseFragment> allfragmentlists = new ArrayList<>();// 全部新闻频道页卡
    private MoreChannelYiXuanAdapter moreChannelYiXuanAdapter;// 已选频道数据适配器
    private MoreChannelQuanBuAdapter moreChannelQuanBuAdapter;// 全部频道数据适配器
    private ImageView iv_back_channelmanage;//标题栏返回按钮

    private MyDBHelper dbhelper;
    private SQLiteDatabase db;
    private MyDBManager dbmanager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channelmanage);
        mygridview_yixuan = (MyGridView) findViewById(R.id.mygridview_yixuan);
        mygridview_quanbu = (MyGridView) findViewById(R.id.mygridview_quanbu);
        iv_back_channelmanage = (ImageView) findViewById(R.id.iv_back_channelmanage);
        // 点击返回
        iv_back_channelmanage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new DBChangeEvent());
                finish();
            }
        });
        settablist();// 设置默认新闻分类
        bindData();
        initEvent();
    }

    // 设置新闻分类
    private void settablist() {
        dbhelper = new MyDBHelper(ChannelManageActivity.this);
        db = dbhelper.getWritableDatabase();
        dbmanager = new MyDBManager(dbhelper, db);
        // 标题栏集合
        tablists = dbmanager.querydata();
        // 标题栏对应的页面集合
        List<BaseFragment> list = new ArrayList<>();
        for (int i = 0; i < tablists.size(); i++) {
            String channelName = tablists.get(i);
            BaseFragment baseFragment = BaseFragment.newInstance(channelName);
            list.add(baseFragment);
        }
//        for (int i = 0; i < tablists.size(); i++) {
//            dbmanager.insertdata(tablists.get(i));
//        }

        moreChannelYiXuanAdapter = new MoreChannelYiXuanAdapter(ChannelManageActivity.this, tablists);
        mygridview_yixuan.setAdapter(moreChannelYiXuanAdapter);
    }

    private void initEvent() {
        // 已选频道
        mygridview_yixuan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position >= 8) {
                    // 点击已选频道下的任一一个分类可将其从已选频道分类下删除
                    dbmanager.deletedata(moreChannelYiXuanAdapter.getItem(position).toString());
                    tablists = dbmanager.querydata();
                    moreChannelYiXuanAdapter = new MoreChannelYiXuanAdapter(ChannelManageActivity.this, tablists);
                    mygridview_yixuan.setAdapter(moreChannelYiXuanAdapter);
                    moreChannelYiXuanAdapter.notifyDataSetChanged();
                }
            }
        });
        // 全部频道
        mygridview_quanbu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 点击全部频道下的任一一个分类可添加至已选频道分类下
                dbmanager.insertdata(moreChannelQuanBuAdapter.getItem(position).toString());
                tablists = dbmanager.querydata();
                moreChannelYiXuanAdapter = new MoreChannelYiXuanAdapter(ChannelManageActivity.this, tablists);
                mygridview_yixuan.setAdapter(moreChannelYiXuanAdapter);
                moreChannelYiXuanAdapter.notifyDataSetChanged();
            }
        });
    }

    // 全部新闻分类
    private void bindData() {
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.ALLWEBURL_url, NetWorkDataUrl.ALLWEBURL_PARAM, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    newsClassEntity = gson.fromJson(result, NewsClassEntity.class);
                    if (newsClassEntity.getShowapi_res_body().getChannelList().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    //Snackbar.make(relativelayout_quanbuxinwen, "服务器君离家出走了~", Snackbar.LENGTH_SHORT).show();
                    break;
                case 1:
                    for (int i = 0; i < newsClassEntity.getShowapi_res_body().getChannelList().size(); i++) {
                        alltablists.add(newsClassEntity.getShowapi_res_body().getChannelList().get(i).getName());
                        String channelName = newsClassEntity.getShowapi_res_body().getChannelList().get(i).getName();
                        BaseFragment baseFragment = BaseFragment.newInstance(channelName);
                        allfragmentlists.add(baseFragment);
                    }

                    moreChannelQuanBuAdapter = new MoreChannelQuanBuAdapter(ChannelManageActivity.this, alltablists);
                    mygridview_quanbu.setAdapter(moreChannelQuanBuAdapter);
                    break;
                case 2:

                    break;
            }
        }
    };


}
